package com.lhy.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.lhy.domain.Brand;
import com.lhy.goods.mapper.BrandMapper;
import com.lhy.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand>
		implements BrandService {
	@Autowired
	private BrandMapper brandMapper;
	//查出所有品牌
	@Override
	public List<Brand> selectAll() {
		LambdaQueryWrapper<Brand> brandLambdaQueryWrapper=new LambdaQueryWrapper<>();
		brandLambdaQueryWrapper.eq(Brand::getIsDelete,0);
		return brandMapper.selectList(brandLambdaQueryWrapper);
	}
	//增加品牌
	@Override
	public Integer insertBrand(Brand newBrand) {
		return brandMapper.insert(newBrand);
	}
	//更新品牌
	@Override
	public Integer updateBrand(Brand newBrand) {
		return brandMapper.updateById(newBrand);
	}
	//逻辑删品牌(多个)
	@Override
	@Transactional
	public Integer delete(String ids) {
		String[] splitId = ids.split(",");
		try {
			for (String id : splitId) {
				Brand brand = new Brand();
				brand.setId(Long.parseLong(id));
				brand.setIsDelete(1);
				brandMapper.updateById(brand);
			}
			return 1;
		} catch (NumberFormatException e) {
			return -1;
		}
	}
	//品牌停用的两种重载
	@Override
	public Integer changeStatus(Long id, Integer status) {
		Brand brand = new Brand();
		brand.setId(id);
		brand.setStatus(status);
		return brandMapper.updateById(brand);
	}

	@Override
	public Integer changeStatus(Long id) {
		Brand brand = brandMapper.selectById(id);
		if (brand==null) {return -1;}
		if (brand.getStatus()==0){
			brand.setStatus(1);
		}else {
			brand.setStatus(0);
		}
		return brandMapper.updateById(brand);
	}
	//条件查询搜索品牌
	@Override
	public List<Brand> selectList(Brand brand) {
		LambdaQueryWrapper<Brand> brandLambdaQueryWrapper =this.getBrandLambdaQueryWrapper(brand);
		return brandMapper.selectList(brandLambdaQueryWrapper);
	}
	//分页查询品牌
	@Override
	public Page<Brand> findPage(Integer current, Integer size) {
		Page<Brand> page= new Page<>();
		page.setCurrent(current);
		page.setSize(size);

		return this.page(page);
	}
	//品牌列表条件+分页查询
	@Override
	public Page<Brand> findPage(Brand brand, Integer current, Integer size) {
		Page<Brand> page= new Page<>();
		page.setCurrent(current);
		page.setSize(size);
		LambdaQueryWrapper<Brand> brandLambdaQueryWrapper = this.getBrandLambdaQueryWrapper(brand);
		return this.page(page,brandLambdaQueryWrapper);
	}


	//根据传入Brand条件返回合适的过滤器
	private LambdaQueryWrapper<Brand> getBrandLambdaQueryWrapper(Brand brand){
		LambdaQueryWrapper<Brand> brandLambdaQueryWrapper=new LambdaQueryWrapper<>();
		if (brand.getId() !=null){
			brandLambdaQueryWrapper.eq(Brand::getId,brand.getId());
		}
		if (brand.getBrandName() !=null){
			brandLambdaQueryWrapper.eq(Brand::getBrandName,brand.getBrandName());
		}
		if (brand.getBrandLogo() !=null){
			brandLambdaQueryWrapper.eq(Brand::getBrandLogo,brand.getBrandLogo());
		}
		if (brand.getStatus() !=null){
			brandLambdaQueryWrapper.eq(Brand::getStatus,brand.getStatus());
		}
		if (brand.getIsDelete() !=null){
			brandLambdaQueryWrapper.eq(Brand::getIsDelete,brand.getIsDelete());
		}
		if (brand.getCreateTime() !=null){
			brandLambdaQueryWrapper.eq(Brand::getCreateTime,brand.getCreateTime());
		}
		if (brand.getUpdateTime() !=null){
			brandLambdaQueryWrapper.eq(Brand::getUpdateTime,brand.getUpdateTime());
		}
		return brandLambdaQueryWrapper;
	}

	@Override
	public List<Map<String,String>> findListByCategoryName(String categoryName) {
		return brandMapper.findListByCategoryName(categoryName);
	}

}